package com.duoqin.guard;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.BaseColumns;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class ChatContacts implements Parcelable {
    private final static String TAG = "duoqin.ChatContacts";

    public static final Uri CONTENT_URI = Uri.parse("content://com.duoqin.guard/chat_contacts");

    //////////////////////////////
    // Parcelable apis
    //////////////////////////////
    public static final Parcelable.Creator<ChatContacts> CREATOR
            = new Parcelable.Creator<ChatContacts>() {
        public ChatContacts createFromParcel(Parcel p) {
            return new ChatContacts(p);
        }

        public ChatContacts[] newArray(int size) {
            return new ChatContacts[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    //////////////////////////////
    // Column definitions
    //////////////////////////////
    public static class Columns implements BaseColumns {
        /**
         * NAME
         * <P>Type: STRING</P>
         */
        public static final String NAME = "name";
        /**
         * OPEN_ID
         * <P>Type: STRING</P>
         */
        public static final String OPEN_ID = "open_id";
        /**
         * IMG
         * <P>Type: STRING</P>
         */
        public static final String IMG = "img";

        static final String[] QUERY_COLUMNS = {
                _ID, NAME, OPEN_ID, IMG
        };

        static final int CHAT_CONTACTS_ID_INDEX = 0;
        static final int CHAT_CONTACTS_NAME_INDEX = 1;
        static final int CHAT_CONTACTS_OPEN_ID_INDEX = 2;
        static final int CHAT_CONTACTS_IMG_INDEX = 3;
    }

    public String name;
    public String openId;
    public String img;

    public ChatContacts() {
        name = null;
        openId = null;
        img = null;
    }

    public ChatContacts(Cursor c) {
        name = c.getString(Columns.CHAT_CONTACTS_NAME_INDEX);
        openId = c.getString(Columns.CHAT_CONTACTS_OPEN_ID_INDEX);
        img = c.getString(Columns.CHAT_CONTACTS_IMG_INDEX);
    }

    public ChatContacts(Parcel p) {
        name = p.readString();
        openId = p.readString();
        img = p.readString();
    }

    @Override
    public void writeToParcel(Parcel p, int flags) {
        p.writeString(name);
        p.writeString(openId);
        p.writeString(img);
    }

    public static List<ChatContacts> getChatContacts(ContentResolver contentResolver) {
        List<ChatContacts> chatContactsList = new ArrayList<>();
        Cursor cursor = contentResolver.query(CONTENT_URI, Columns.QUERY_COLUMNS, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            do {
                ChatContacts chatContacts = new ChatContacts(cursor);
                chatContactsList.add(chatContacts);
            } while (cursor.moveToNext());
        }
        if (cursor != null) cursor.close();
        Log.v(TAG, "getChatContacts count " + chatContactsList.size());
        return chatContactsList;
    }

    public static void insertOrUpdateChatContacts(ContentResolver contentResolver,
                                                  List<ChatContacts> chatContactsList) {
        if (chatContactsList != null && chatContactsList.size() > 0) {
            for (ChatContacts chatContacts : chatContactsList) {
                ContentValues contentValues = new ContentValues();
                contentValues.put(Columns.NAME, chatContacts.name);
                contentValues.put(Columns.OPEN_ID, chatContacts.openId);
                contentValues.put(Columns.IMG, chatContacts.img);
                if (updateChatContact(contentResolver, contentValues, chatContacts.openId) == 0) {
                    insertChatContact(contentResolver, contentValues);
                }
            }
        }
        contentResolver.notifyChange(CONTENT_URI, null);
    }

    private static Uri insertChatContact(ContentResolver contentResolver,
                                         ContentValues contentValues) {
        Uri uri = contentResolver.insert(CONTENT_URI, contentValues);
        Log.v(TAG, "insertChatContact: " + uri);
        return uri;
    }

    private static int updateChatContact(ContentResolver contentResolver,
                                         ContentValues contentValues, String openId) {
        int count = contentResolver.update(CONTENT_URI, contentValues, Columns.OPEN_ID + "=?",
                new String[] {openId});
        Log.v(TAG, "updateChatContact: " + count);
        return count;
    }
}
